﻿package com.farm.code.web.action;

import com.farm.code.domain.FarmCodeProject;
import com.farm.code.domain.FarmCodeType;
import com.farm.code.server.FarmCodeProjectManagerInter;
import com.farm.code.server.FarmCodeTypeManagerInter;

import com.farm.web.easyui.EasyUiUtils;
import java.util.Map;
import com.farm.core.page.CommitType;
import com.farm.core.page.PageSet;
import com.farm.core.page.PageType;
import com.farm.core.sql.query.DBRule;
import com.farm.core.sql.query.DataQuery;
import com.farm.core.sql.result.DataResult;
import com.farm.doc.server.FarmFileManagerInter;
import com.farm.web.WebSupport;
import com.farm.web.spring.BeanFactory;

/**
 * 工程
 * 
 * @author autoCode
 * 
 */
public class ActionFarmCodeProjectQuery extends WebSupport {
	private Map<String, Object> jsonResult;// 结果集合
	private DataQuery query;// 条件查询
	private FarmCodeProject entity;// 实体封装
	private FarmCodeType type;// 实体封装
	private PageSet pageset;// 请求状态
	private String ids;// 主键集合
	private String parentId;

	/**
	 * 查询结果集合
	 * 
	 * @return
	 */
	public String queryall() {
		try {
			query = EasyUiUtils.formatGridQuery(getRequest(), query);
			if (parentId != null) {
				query.addRule(new DBRule("TYPEID", parentId, "="));
			}
			DataResult result = aloneIMP
					.createFarmCodeProjectSimpleQuery(query).search();
			result.runDictionary("1:可用,0:禁用", "PSTATE");
			result.runDictionary("0:数据库向导,1:功能向导", "TYPE");
			jsonResult = EasyUiUtils.formatGridData(result);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		return SUCCESS;
	}

	/**
	 * 提交修改数据
	 * 
	 * @return
	 */
	public String editSubmit() {
		try {
			if (entity.getGraphid() != null&&entity.getGraphid() .trim().length()>0) {
				fileIMP.cancelFile(entity.getGraphid());
			}
			entity = aloneIMP.editFarmCodeProjectEntity(entity,
					getCurrentUser());
			if (entity.getGraphid() != null&&entity.getGraphid() .trim().length()>0) {
				fileIMP.submitFile(entity.getGraphid());
			}
			pageset = new PageSet(PageType.UPDATE, CommitType.TRUE);
		} catch (Exception e) {
			pageset = PageSet.initPageSet(pageset, PageType.UPDATE,
					CommitType.FALSE, e);
		}
		return SUCCESS;
	}

	/**
	 * 提交新增数据
	 * 
	 * @return
	 */
	public String addSubmit() {
		try {
			entity = aloneIMP.insertFarmCodeProjectEntity(entity,
					getCurrentUser());
			if (entity.getGraphid() != null&&entity.getGraphid() .trim().length()>0) {
				fileIMP.submitFile(entity.getGraphid());
			}
			pageset = new PageSet(PageType.ADD, CommitType.TRUE);
		} catch (Exception e) {
			pageset = PageSet.initPageSet(pageset, PageType.ADD,
					CommitType.FALSE, e);
		}
		return SUCCESS;
	}

	/**
	 * 删除数据
	 * 
	 * @return
	 */
	public String delSubmit() {
		// TODO 自动生成代码,修改后请去除本注释
		try {
			for (String id : parseIds(ids)) {
				aloneIMP.deleteFarmCodeProjectEntity(id, getCurrentUser());
			}
			pageset = new PageSet(PageType.ADD, CommitType.TRUE);
		} catch (Exception e) {
			pageset = PageSet.initPageSet(pageset, PageType.DEL,
					CommitType.FALSE, e);
		}
		return SUCCESS;
	}

	/**
	 * 跳转
	 * 
	 * @return
	 */
	public String forSend() {
		// TODO 自动生成代码,修改后请去除本注释
		return SUCCESS;
	}

	/**
	 * 显示详细信息（修改或浏览时）
	 * 
	 * @return
	 */
	public String view() {
		try {
			if (parentId != null) {
				type = typeIMP.getFarmCodeTypeEntity(parentId);
			}
			switch (pageset.getPageType()) {
			case (1): {// 新增
				return SUCCESS;
			}
			case (0): {// 展示
				entity = aloneIMP.getFarmCodeProjectEntity(ids);
				type = typeIMP.getFarmCodeTypeEntity(entity.getTypeid());
				return SUCCESS;
			}
			case (2): {// 修改
				entity = aloneIMP.getFarmCodeProjectEntity(ids);
				type = typeIMP.getFarmCodeTypeEntity(entity.getTypeid());
				return SUCCESS;
			}
			default:
				break;
			}
		} catch (Exception e) {
			pageset = PageSet.initPageSet(pageset, PageType.OTHER,
					CommitType.FALSE, e);
		}
		return SUCCESS;
	}

	private final static FarmCodeProjectManagerInter aloneIMP = (FarmCodeProjectManagerInter) BeanFactory
			.getBean("farm_code_projectProxyId");
	private final static FarmCodeTypeManagerInter typeIMP = (FarmCodeTypeManagerInter) BeanFactory
			.getBean("farm_code_typeProxyId");
	private final static FarmFileManagerInter fileIMP = (FarmFileManagerInter) BeanFactory
	.getBean("farm_docFileProxyId");
	// ----------------------------------------------------------------------------------
	public DataQuery getQuery() {
		return query;
	}

	public void setQuery(DataQuery query) {
		this.query = query;
	}

	public FarmCodeProject getEntity() {
		return entity;
	}

	public void setEntity(FarmCodeProject entity) {
		this.entity = entity;
	}

	public PageSet getPageset() {
		return pageset;
	}

	public void setPageset(PageSet pageset) {
		this.pageset = pageset;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public Map<String, Object> getJsonResult() {
		return jsonResult;
	}

	public void setJsonResult(Map<String, Object> jsonResult) {
		this.jsonResult = jsonResult;
	}

	public String getParentId() {
		return parentId;
	}

	public void setParentId(String parentId) {
		this.parentId = parentId;
	}

	public FarmCodeType getType() {
		return type;
	}

	public void setType(FarmCodeType type) {
		this.type = type;
	}

//	private static final Logger log = Logger
//			.getLogger(ActionFarmCodeProjectQuery.class);
	private static final long serialVersionUID = 1L;
	/**
	 * 加载树节点 public String loadTreeNode() { treeNodes =
	 * EasyUiTreeNode.formatAjaxTree(EasyUiTreeNode .queryTreeNodeOne(id,
	 * "SORT", "alone_menu", "ID", "PARENTID", "NAME").getResultList(),
	 * EasyUiTreeNode .queryTreeNodeTow(id, "SORT", "alone_menu", "ID",
	 * "PARENTID", "NAME").getResultList(), "PARENTID", "ID", "NAME"); return
	 * SUCCESS; }
	 * 
	 * @return
	 */
}
